package com.huangpuweb.task.controller;

import com.huangpuweb.task.Constants;
import com.huangpuweb.task.model.SysHpRoadPrediction;
import com.huangpuweb.task.model.ZyRidPredShort;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.service.SyncZYJJZDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * 同步道路预测数据定时任务
 */
@Controller
public class SyncRoadPredDataJob {

    private static Logger logger = Logger.getLogger(SyncRoadPredDataJob.class);


    private SyncSysDataService syncSysDataService;

    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }

    private SyncZYJJZDataService syncZYJJZDataService;
    @Autowired
    public void setSyncZYJJZDataService(SyncZYJJZDataService syncZYJJZDataService) {
        this.syncZYJJZDataService = syncZYJJZDataService;
    }


    public void SyncRoadPredDataJobTask() throws Exception {
        LogFactory.getLog(this.getClass()).info("--------start-------SyncRoadPredDataJobTask----------" + new Date());

        /**
         * 同步各个景区路况信息到表hp_road_condition
         */
        String[] spotcoderoads = Constants.SPOTCODEROAD.split(",");

        String updateTime = DateUtil.getDateTime();
        for (String spotCode : spotcoderoads) {


            List<String> roadIds=syncSysDataService.getRoadIdsBySpotCode(spotCode);

            List<ZyRidPredShort> listZyRidPredShort=syncZYJJZDataService.getListZyRidPredShort(roadIds,roadIds.size());


            for (ZyRidPredShort record : listZyRidPredShort) {

                SysHpRoadPrediction prediction = new SysHpRoadPrediction();
                prediction.setSpotCode(spotCode);
                //设置道路id
                prediction.setRoadId(record.getRid());
                //设置路段拥堵延时指数
                prediction.setCongestionIndex(record.getCongestion_index());
                //设置路段速度
                prediction.setSpeed(record.getSpeed());
                //设置时间片序号
                prediction.setStepIndex(Integer.valueOf(record.getStep_index()));
                //设置预测时间

                prediction.setPredTime(record.getPred_time());
                //设置路段流入流量
                prediction.setVolIn(record.getVol_in());
                //设置更新时间
                prediction.setUpdateTime(updateTime);

                syncSysDataService.insertOrUpdateRoadPrediction(prediction);
//                Hp_road_prediction _prediction = dao().fetch(Hp_road_prediction.class, Cnd.where("roadId", "=", rid).and("predTime", "=", predTime));
//                if (_prediction == null) {
//                    log.info("不存在该条景区周边道路预测数据，新增到数据库");
//                    dao().insert(prediction);
//                }
            }

        }
        LogFactory.getLog(this.getClass()).info("--------end-------SyncRoadPredDataJobTask----------" + new Date());

    }


    public void AliPreNjdlTask(){
        String content = "";
        LogFactory.getLog(this.getClass()).info("------AliPreNjdlTask-----start-------");
        try {
            String url = "http://215.0.0.31:8085/pred/global_estimate/area/njdl";
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            HttpResponse response = httpClient.execute(get);
            content = EntityUtils.toString(response.getEntity());
            syncSysDataService.insertAlibabaPred(content.trim(),DateUtil.getDateTime());
        } catch (IOException e) {
            LogFactory.getLog(this.getClass()).info("------AliPreNjdlTask-----error-------"+e);
        }
    }


}
